{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Atom featurizers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from chemprop.featurizers.atom import MultiHotAtomFeaturizer"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This is an example atom to featurize."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from rdkit import Chem\n",
    "\n",
    "atom_to_featurize = Chem.MolFromSmiles(\"CC\").GetAtoms()[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Atom features"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The following atom features are generated by `rdkit` and cast to one-hot vectors (except for mass which is divided by 100). These feature vectors are joined together to a single multi-hot feature vector (with a final float32 bit for mass). All of these features (except aromaticity and mass) are padded with an extra bit for all unknown values.\n",
    "\n",
    " - atomic number\n",
    " - degree\n",
    " - formal charge\n",
    " - chiral tag\n",
    " - number of hydrogens\n",
    " - hybridization\n",
    " - aromaticity\n",
    " - mass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### v2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The v2 atom featurizer is the default. It provides bits in the feature vector for:\n",
    "\n",
    " - atomic number\n",
    "    - first four rows of the period table plus iodine\n",
    " - degree\n",
    "    - 0 bonds - 5 bonds\n",
    " - formal charge\n",
    "    - -2, -1, 0, 1, 2\n",
    " - chiral tag\n",
    "    - 0, 1, 2, 3 - See `rdkit.Chem.rdchem.ChiralType` for more details\n",
    " - number of hydrogens\n",
    "    - 0 - 4\n",
    " - hybridization\n",
    "    - S, SP, SP2, SP2D, SP3, SP3D, SP3D2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.     , 0.     , 0.     , 0.     , 0.     , 1.     , 0.     ,\n",
       "       0.     , 0.     , 0.     , 0.     , 0.     , 0.     , 0.     ,\n",
       "       0.     , 0.     , 0.     , 0.     , 0.     , 0.     , 0.     ,\n",
       "       0.     , 0.     , 0.     , 0.     , 0.     , 0.     , 0.     ,\n",
       "       0.     , 0.     , 0.     , 0.     , 0.     , 0.     , 0.     ,\n",
       "       0.     , 0.     , 0.     , 0.     , 0.     , 0.     , 0.     ,\n",
       "       1.     , 0.     , 0.     , 0.     , 0.     , 0.     , 0.     ,\n",
       "       1.     , 0.     , 1.     , 0.     , 0.     , 0.     , 0.     ,\n",
       "       0.     , 0.     , 0.     , 1.     , 0.     , 0.     , 0.     ,\n",
       "       0.     , 0.     , 0.     , 1.     , 0.     , 0.     , 0.     ,\n",
       "       0.     , 0.12011])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "featurizer = MultiHotAtomFeaturizer.v2()\n",
    "featurizer(atom_to_featurize)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### v1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The v1 atom featurizer is the same as was used in Chemprop v1. It is the same as the v2 atom featurizer except for:\n",
    "\n",
    " - atomic number\n",
    "    - first 100 elements (customizable)\n",
    " - hybridization\n",
    "    - SP, SP2, SP3, SP3D, SP3D2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.     , 0.     , 0.     , 0.     , 0.     , 1.     , 0.     ,\n",
       "       0.     , 0.     , 0.     , 0.     , 0.     , 0.     , 0.     ,\n",
       "       0.     , 0.     , 0.     , 0.     , 0.     , 0.     , 0.     ,\n",
       "       0.     , 0.     , 0.     , 0.     , 0.     , 0.     , 0.     ,\n",
       "       0.     , 0.     , 0.     , 0.     , 0.     , 0.     , 0.     ,\n",
       "       0.     , 0.     , 0.     , 0.     , 0.     , 0.     , 0.     ,\n",
       "       0.     , 0.     , 0.     , 0.     , 0.     , 0.     , 0.     ,\n",
       "       0.     , 0.     , 0.     , 0.     , 0.     , 0.     , 0.     ,\n",
       "       0.     , 0.     , 0.     , 0.     , 0.     , 0.     , 0.     ,\n",
       "       0.     , 0.     , 0.     , 0.     , 0.     , 0.     , 0.     ,\n",
       "       0.     , 0.     , 0.     , 0.     , 0.     , 0.     , 0.     ,\n",
       "       0.     , 0.     , 0.     , 0.     , 0.     , 0.     , 0.     ,\n",
       "       0.     , 0.     , 0.     , 0.     , 0.     , 0.     , 0.     ,\n",
       "       0.     , 0.     , 0.     , 0.     , 0.     , 0.     , 0.     ,\n",
       "       0.     , 0.     , 0.     , 0.     , 0.     , 0.     , 0.     ,\n",
       "       1.     , 0.     , 0.     , 0.     , 0.     , 0.     , 0.     ,\n",
       "       1.     , 0.     , 1.     , 0.     , 0.     , 0.     , 0.     ,\n",
       "       0.     , 0.     , 0.     , 1.     , 0.     , 0.     , 0.     ,\n",
       "       0.     , 1.     , 0.     , 0.     , 0.     , 0.     , 0.12011])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "featurizer = MultiHotAtomFeaturizer.v1()\n",
    "featurizer(atom_to_featurize)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.     , 0.     , 0.     , 0.     , 0.     , 1.     , 0.     ,\n",
       "       0.     , 0.     , 0.     , 0.     , 0.     , 0.     , 0.     ,\n",
       "       0.     , 0.     , 0.     , 0.     , 0.     , 0.     , 0.     ,\n",
       "       0.     , 0.     , 0.     , 0.     , 0.     , 0.     , 0.     ,\n",
       "       0.     , 0.     , 0.     , 0.     , 0.     , 0.     , 0.     ,\n",
       "       0.     , 0.     , 0.     , 0.     , 0.     , 0.     , 0.     ,\n",
       "       0.     , 0.     , 0.     , 0.     , 0.     , 0.     , 0.     ,\n",
       "       0.     , 0.     , 0.     , 0.     , 0.     , 0.     , 0.     ,\n",
       "       0.     , 0.     , 1.     , 0.     , 0.     , 0.     , 0.     ,\n",
       "       0.     , 0.     , 1.     , 0.     , 1.     , 0.     , 0.     ,\n",
       "       0.     , 0.     , 0.     , 0.     , 0.     , 1.     , 0.     ,\n",
       "       0.     , 0.     , 0.     , 1.     , 0.     , 0.     , 0.     ,\n",
       "       0.     , 0.12011])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "featurizer = MultiHotAtomFeaturizer.v1(max_atomic_num=53)\n",
    "featurizer(atom_to_featurize)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### organic"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The organic atom featurizer is optimized to reduce feature vector size for organic molecule. It is the same as the v2 atom featurizer except for:\n",
    "\n",
    " - atomic number\n",
    "    - H, B, C, N, O, F, Si, P, S, Cl, Br, and I atoms\n",
    " - hybridization\n",
    "    - S, SP, SP2, SP3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.     , 0.     , 1.     , 0.     , 0.     , 0.     , 0.     ,\n",
       "       0.     , 0.     , 0.     , 0.     , 0.     , 0.     , 0.     ,\n",
       "       0.     , 0.     , 0.     , 1.     , 0.     , 0.     , 0.     ,\n",
       "       0.     , 0.     , 0.     , 1.     , 0.     , 1.     , 0.     ,\n",
       "       0.     , 0.     , 0.     , 0.     , 0.     , 0.     , 1.     ,\n",
       "       0.     , 0.     , 0.     , 0.     , 0.     , 1.     , 0.     ,\n",
       "       0.     , 0.12011])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "featurizer = MultiHotAtomFeaturizer.organic()\n",
    "featurizer(atom_to_featurize)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Custom"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Custom atom featurizers can also be created by specifying the choices. Custom choices for atomic number, degree, formal charge, chiral tag, # of hydrogens, and hybridization can be specified to create a custom atom featurizer. Aromaticity featurization is always True/False. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.     , 1.     , 0.     , 0.     , 0.     , 0.     , 0.     ,\n",
       "       0.     , 0.     , 1.     , 0.     , 0.     , 0.     , 1.     ,\n",
       "       0.     , 0.     , 0.     , 1.     , 0.     , 0.     , 0.     ,\n",
       "       0.     , 0.     , 0.     , 0.     , 1.     , 0.     , 0.     ,\n",
       "       0.     , 0.     , 1.     , 0.     , 0.     , 0.12011])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from rdkit.Chem.rdchem import HybridizationType\n",
    "\n",
    "atomic_nums = [1, 6, 7, 8]\n",
    "degrees = [0, 1, 2, 3, 4]\n",
    "formal_charges = [-2, -1, 0, 1, 2]\n",
    "chiral_tags = [0, 1, 2, 3]\n",
    "num_Hs = [0, 1, 2, 3, 4]\n",
    "hybridizations = [HybridizationType.SP, HybridizationType.SP2, HybridizationType.SP3]\n",
    "featurizer = MultiHotAtomFeaturizer(\n",
    "    atomic_nums, degrees, formal_charges, chiral_tags, num_Hs, hybridizations\n",
    ")\n",
    "featurizer(atom_to_featurize)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Generic"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Any class that has a length and returns a numpy array when given an `rdkit.Chem.rdchem.Atom` can be used as an atom featurizer. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([6.])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from rdkit.Chem.rdchem import Atom\n",
    "import numpy as np\n",
    "\n",
    "\n",
    "class MyAtomFeaturizer:\n",
    "    def __len__(self):\n",
    "        return 1\n",
    "\n",
    "    def __call__(self, a: Atom):\n",
    "        return np.array([a.GetAtomicNum()], dtype=float)\n",
    "\n",
    "\n",
    "featurizer = MyAtomFeaturizer()\n",
    "featurizer(atom_to_featurize)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "chemprop",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
